<template>
  <el-form :label-position="labelPosition" label-width="auto" :model="formLabelAlign" style="max-width: 600px">
    <br>
    <el-form-item label="商品ID：" :label-position="itemLabelPosition">
      <el-input v-model="formLabelAlign.proid" />
    </el-form-item>
    <el-form-item label="商品名称：" :label-position="itemLabelPosition">
      <el-input v-model="formLabelAlign.proname" />
    </el-form-item>
    <el-form-item label="上传图片：" :label-position="itemLabelPosition">
      <el-upload class="avatar-uploader" action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
        :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <el-icon v-else class="avatar-uploader-icon">
          <Plus />
        </el-icon>
      </el-upload>
    </el-form-item>
    <el-form-item label="商品分类：" :label-position="itemLabelPosition">
      <el-input v-model="formLabelAlign.category" />
    </el-form-item>
    <el-form-item label="商品介绍：" :label-position="itemLabelPosition">
      <el-input v-model="formLabelAlign.desc" />
    </el-form-item>
    <el-form-item label="价格：" :label-position="itemLabelPosition">
      <el-input v-model="formLabelAlign.originprice" />
    </el-form-item>
    <el-form-item label="库存：" :label-position="itemLabelPosition">
      <el-input v-model="formLabelAlign.stock" />
    </el-form-item>
  </el-form>

  <div class="mb-4">
    <el-button type="primary" @click="fabu()">发布商品</el-button>
    <el-button type="danger" @click="quxiao">取消</el-button>
  </div>

</template>
<script setup lang="ts">
import { reactive, ref } from 'vue'
import type { FormItemProps, FormProps } from 'element-plus'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import type { UploadProps } from 'element-plus'
const labelPosition = ref<FormProps['labelPosition']>('right')
const itemLabelPosition = ref<FormItemProps['labelPosition']>('')
const formLabelAlign = reactive({
  proid: '',
  proname: '',
  img: [],
  category: '',
  desc: '',
  originprice: '',
  stock: '',
})
const imageUrl = ref('')
const handleAvatarSuccess: UploadProps['onSuccess'] = (
  response,
  uploadFile
) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw!)
}
const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  return true
}
//取消按钮
function quxiao() {
  formLabelAlign.proid = ''
  formLabelAlign.proname = ''
  formLabelAlign.img = []
  formLabelAlign.category = ''
  formLabelAlign.desc = ''
  formLabelAlign.originprice = ''
  formLabelAlign.stock = ''
}


//发布按钮及发布商品接口
async function fabu() {

}

</script>
<style lang="css" scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #45474a;
  width: 80px;
  height: 80px;
  border: #9d9ea0 1px dashed;
  text-align: center;
}
</style>
